package GUI;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import java.awt.SystemColor;
import java.awt.Window.Type;

/**
 * This class get password and userName from client and open a user form.  
 * @author michael
 *	@author matan
 */
public class LoginForm extends JFrame
{
	private JLabel usernameLabel = null;
	private JLabel  passwordLabel = null;
	private JTextField usernameField = null;
	private JPasswordField passwordField = null;
	private JButton loginButton = null;
	private JPanel jContentPane = null;
	private MaketDepardmentForm maketdepardmentForm = null;
	private ClientContactsFORM clientcontactsFORM = null;
	private MarketManagerFORM marketmanagerFORM = null;	
	private SalesManForm salesmanForm = null;		
	
	/**
	 * This is the default constructor
	 */
	public LoginForm() 
	{
		super();
		initialize();		
	}
	
	/**
	 * This method initializes AcademicFrame 
	 */
	private void initialize()
	{
		this.setSize(400,300);
		this.setContentPane(getJContentPane());
		this.setTitle("CCRM SYSTEM - LOG IN");
		this.maketdepardmentForm = new MaketDepardmentForm();
		this.clientcontactsFORM = new ClientContactsFORM();
		this.marketmanagerFORM = new MarketManagerFORM();		
	}
	/**
	 * This method initializes LoginForm
	 */
	private JPanel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.activeCaption);
			jContentPane.setLayout(null);
			usernameLabel  = new JLabel();
			usernameLabel .setBounds(new Rectangle(55, 35, 113, 22));
			usernameLabel .setText("User name");
			
			passwordLabel = new JLabel();
			passwordLabel.setBounds(new Rectangle(57, 70, 113, 22));
			passwordLabel.setText("Password");
			
			jContentPane.add(usernameLabel, null);
			jContentPane.add(passwordLabel, null);
			jContentPane.add(getUsernameField(), null);
			jContentPane.add(getPasswordField(), null);
			jContentPane.add(getloginButton(),null);
		}
		return jContentPane;		
	}
	
	/**
	 * This method initializes usernameField	
	 */
	private JTextField getUsernameField()
	{
		if (usernameField == null) 
		{
			usernameField = new JTextField();
			usernameField.setBounds(new Rectangle(142, 35, 130, 22));
		}
		return usernameField;
	}
	
	/**
	 * This method initializes passwordField	
	 */
	private JPasswordField getPasswordField()
	{
		if (passwordField == null) 
		{
			passwordField = new JPasswordField();
			passwordField.setBounds(new Rectangle(142, 70, 130, 22));
		}
		return passwordField;
	}
	
	/**
	 * This method initializes loginButton	
	 */
	private JButton getloginButton() 
	{
		
		if (loginButton == null) 
		{
			loginButton = new JButton();
			loginButton.setBounds(new Rectangle(153, 114, 110, 29));
			loginButton.setText("Log in");
			loginButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String message = "";
					try
					{
						String username = usernameField.getText();
						char[] password = passwordField.getPassword();
						if (!username.equals("") && (password.length > 0))
						{
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							message = (String)connectiontoserver.userCheck(username, password);
							while(true)
							{	
								if (message != null)
								{
									if (message.equals("AlreadyIN")) 
									{
										JOptionPane.showMessageDialog(null, "You already LOGGED IN!!!","",JOptionPane.WARNING_MESSAGE);
										break;
									}	
									if (message.equals("Ilegalusername"))
									{
										JOptionPane.showMessageDialog(null, "Elegal user name or password","",JOptionPane.WARNING_MESSAGE);
										break;
									}	
									if (message.equals("MarketDepardment"))
									{	
										setContentPane(maketdepardmentForm);
										break;
									}
									if (message.equals("ClientContact"))
									{	
										setContentPane(clientcontactsFORM);
										break;
									}
									if (message.equals("MarketManager"))
									{	
										setContentPane(marketmanagerFORM);
										break;
									}		
									if (message.equals("SellsMen"))
									{	
										setSize(550,580);
										salesmanForm = new SalesManForm(username);
										setContentPane(salesmanForm);
										break;
									}		
								}
								else
									JOptionPane.showMessageDialog(null, "ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
								break;
							}	
						}
						else
							JOptionPane.showMessageDialog(null, "ILEGAL INPUT", "WARNING!!!",JOptionPane.WARNING_MESSAGE);
						}catch (ClientNotInitializesException notinitializesexception)
						{
							JOptionPane.showMessageDialog(null, "ERROR","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
					}catch (Exception errorexception)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);						
					}							
				}
			});
		}
		return loginButton;
	}
	
}
